/**
 * meiguang8.com
 * Copyright (C) 2016-2019 All Rights Reserved.
 */
package com.yuanchuang.yp.mall.web.admin.common.config.mybatis;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

/**
 * class_name: SpringMybatisPlusConfig
 * package: com.yuanchuang.yp.mall.web.admin.common.config.mybatis
 * describe: TODO
 * @author: yuanyaun
 * creat_date: 2019/7/16
 * creat_time: 15:29
 **/
@Configuration
public class SpringMybatisPlusConfig {

    /**
     * Mapper接口所在包名，Spring会自动查找其下的类   接口扫描
     *
     * @return
     */
    @Bean
    public static MapperScannerConfigurer lifeCircleConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.yuanchuang.yp.mall.web.admin.**.mapper.**.**");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("lifeCircleSqlSessionFactory");
        return mapperScannerConfigurer;
    }


    @ConfigurationProperties(prefix = "spring.datasource.druid")
    @Bean(initMethod = "init", destroyMethod = "close")
    public DruidDataSource lifeCircleDataSource() {
        DruidDataSource druidDataSource = DataSourceBuilder.create().type(DruidDataSource.class).build();
        //  配置初始化大小、最小、最大
        druidDataSource.setInitialSize(1);
        druidDataSource.setMinIdle(1);
        druidDataSource.setMaxActive(64);
        //  配置获取连接等待超时的时间
        druidDataSource.setMaxWait(60000);
        //  配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
        //  配置一个连接在池中最小生存的时间，单位是毫秒
        druidDataSource.setMinEvictableIdleTimeMillis(300000);
        druidDataSource.setValidationQuery("select  1 from dual");
        druidDataSource.setTestWhileIdle(true);
        druidDataSource.setTestOnBorrow(false);
        druidDataSource.setTestOnReturn(false);
        //  打开PSCache，并且指定每个连接上PSCache的大小
        druidDataSource.setPoolPreparedStatements(true);
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
        return druidDataSource;
    }


    /**
     * (事务管理)transaction manager, use JtaTransactionManager for global tx
     *
     * @return
     */
    @Bean
    public DataSourceTransactionManager mgTransactionManager() throws SQLException {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(this.lifeCircleDataSource());
        return dataSourceTransactionManager;
    }

    /**
     * spring和MyBatis完美整合，不需要mybatis的配置映射文件
     *
     * @return
     */
    @Bean
    public MybatisSqlSessionFactoryBean lifeCircleSqlSessionFactory() throws IOException, SQLException {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(this.lifeCircleDataSource());
        // 设置查找器
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //扫描mapper文件
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mappers/*/*.xml"));
        // 分页插件
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        Interceptor[] plugins = {paginationInterceptor};
        sqlSessionFactoryBean.setPlugins(plugins);

        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "false");
        //配置mysql数据库的方言
        properties.setProperty("dialect", "mysql");
        pageHelper.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});

        return sqlSessionFactoryBean;
    }


}
